import javax.swing.*;
import java.awt.event.*;

public class gridUI extends JFrame {
	
	private JPanel gridTestPanel; // test
	private JButton gridBackButton; // back button
	private JButton tableFromGrid; // button that links to table from the grid
	final int WINDOW_WIDTH = 800;
	final int WINDOW_HEIGHT = 800;
	private initUI init;
	private tableUI table;
	
	// Accessors For variables
	public JPanel getGridTestPanel() { return gridTestPanel; }
	public JButton getGridBackButton() { return gridBackButton; }
	public JButton getTableFromGrid() { return tableFromGrid; }
	public initUI getInit() { return init; }
	public tableUI getTable() { return table; }
	
	// Mutators for Variables
	public void setGridTestPanel(JPanel x) { gridTestPanel = x; }
	public void setGridBackButton(JButton x) { gridBackButton = x; }
	public void setTableFromGrid(JButton x) { tableFromGrid = x; }
	public void setInit(initUI x) { init = x; }
	public void setTable(tableUI x) { table = x; }
	
	public gridUI(initUI initial, tableUI tab){
		
		setTitle("2-D Grid"); // sets window title
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // sets window size
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close when red X is clicked
		buildGridTestPanel();
		add(gridTestPanel); // adds grid panel to frame
		
		init = initial;
		table = tab;
	}
	
	private void buildGridTestPanel(){
		
		gridBackButton = new JButton("Back to Menu"); // creates back to menu button
		gridBackButton.addActionListener(new backListener()); //adds listener to back to menu button
		tableFromGrid = new JButton("Table"); // creates table button
		tableFromGrid.addActionListener(new backListener()); // adds listener to table button
		
		gridTestPanel = new JPanel(); // create panel
		gridTestPanel.add(gridBackButton); // add back button to grid
		gridTestPanel.add(tableFromGrid); // add table button to grid
	}
	
	private class backListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
			if (e.getSource() == gridBackButton){ // if back button was pressed
				setVisible(false); // remove grid visibility
				init.setVisible(true); // make initial GUI visible
			}
			else if(e.getSource() == tableFromGrid){ // if table button was pressed
				
				setVisible(false); // remove grid visibility
				table.setVisible(true); // make table gui visible
			}
		}
	}
}
